#!/bin/bash
set -e
#验证root身份。如非root用户弹出提示。
function root_ness(){
    if [[ $EUID -ne 0 ]]; then
		whiptail --title "Warning" --msgbox "Error:This script must be run as root!" 10 60
		exit 1
    fi
}

function chksystem(){
	VERSION=$(cat /etc/redhat-release |awk '{print $3}'|awk -F "." '{print $1}')
    if [[ $VERSION = 5 ]] || [[ $VERSION = 6 ]]; then
		if [ ! -f "/etc/yum.repos.d/epel.repo" ]; then
			curl -s http://mirrors.aliyun.com/repo/epel-$VERSION.repo > /etc/yum.repos.d/epel.repo
			yum -q makecache &
		fi
	else
		whiptail --title "Warning" --msgbox "Error:This script must be run on Centos-5 or Centos-6!" 10 60
		exit 1
	fi
}

# 选择安装路径，消息框 输入
function select_directory(){
	DIR=$(whiptail --title "Select Directory" --inputbox "Please select an installation directory:  \n[default /app]" 10 60 /app 3>&1 1>&2 2>&3) 
	exitstatus=$?
	if [ $exitstatus = 0 ]; then
		if (whiptail --title "Select Directory" --yes-button " Continue " --no-button "Back" --yesno "The installation directory is $DIR." 10 60) then
			:
		else
			select_directory
		fi
	else
		DIR="/app"
	fi
	if [ "${DIR:0:1}" = "/" ];then
		:
	else
		whiptail --title "Warning" --msgbox "Error:The directory needs to start with /" 10 60
		select_directory
	fi
	mkdir -p $DIR/downloads
}

# 通用-开始优化 顺序0 消息框 多选
function start_all(){
	root_ness
	chksystem
	select_directory
	select_package
}

function select_package(){
DISTROS=$(whiptail --title "Test Checklist Dialog" --checklist \
"Choose Package to Install " 14 60 7 \
install_tengine		"tengine-2.1.2" ON \
install_php			"php-5.4.44" ON \
install_phpize		"phpize-2.2.7" ON \
install_mysql		"mysql_server-5.5.49" ON \
install_redis		"redis-2.6.17" ON \
install_memcached	"memcached-1.4.25" OFF \
install_mongodb		"mongodb-3.2.6" OFF 3>&1 1>&2 2>&3)
exitstatus=$?
if [ $exitstatus = 0 ]; then
    {
		for i in `echo $DISTROS`;do
			install_package
			i=$(echo $i|awk -F "\"" '{print $2}')
			$i
		done
	} | whiptail --gauge "Please wait while installing" 6 60 0
else
    echo "You chose Cancel."
fi
}
function install_package(){
	yum -y -q install wget cmake make bison gcc-c++ ncurses gcc gcc-c++ openssl openssl-devel tcl libmcrypt libmcrypt-devel mcrypt mhash gcc automake autoconf libtool make gcc gcc-c++ glibc libxslt-devel libjpeg libjpeg-devel libpng libpng-devel freetype freetype-devel libxml2 libxml2-devel zlib zlib-devel glibc glibc-devel glib2 glib2-devel bzip2 bzip2-devel ncurses ncurses-devel curl curl-devel e2fsprogs e2fsprogs-devel krb5-devel libidn libidn-devel openssl openssl-devel >/dev/null
}

function install_pcre(){
	cd $DIR/downloads
		rm pcre-8.38.tar.gz -f
		wget http://59.83.33.68/tizone/pcre-8.38.tar.gz 
		tar zxvf pcre-8.38.tar.gz
		cd pcre-8.38
			./configure --prefix=/usr/local/pcre
			make
			make install
}

function install_tengine(){
	install_pcre
	cd $DIR/downloads
		rm tengine-2.1.2.tar.gz -f
		wget http://59.83.33.68/tizone/tengine-2.1.2.tar.gz
		tar zxvf tengine-2.1.2.tar.gz
		cd tengine-2.1.2
			./configure --user=nginx --group=nginx --prefix=$DIR/tengine --with-pcre=$DIR/downloads/pcre-8.38 --with-pcre-jit --with-http_concat_module --with-http_concat_module=shared --with-http_stub_status_module --with-http_ssl_module --with-http_gzip_static_module
			make
			make install
		groupadd nginx
		useradd -g nginx nginx
}

function install_php(){
	cd $DIR/downloads
		rm php-5.4.44.tar.gz -f
		wget http://59.83.33.68/tizone/php-5.4.44.tar.gz
		tar zxvf php-5.4.44.tar.gz
		cd php-5.4.44
			./configure --prefix=$DIR/php  --enable-fpm --with-mcrypt --enable-mbstring --disable-pdo --with-curl --disable-debug  --disable-rpath --enable-inline-optimization --with-bz2  --with-zlib --enable-sockets --enable-sysvsem --enable-sysvshm --enable-pcntl --enable-mbregex --with-mhash --enable-zip --with-pcre-regex --with-mysql --with-mysqli --with-gd --with-jpeg-dir
			make all install
		echo -e "export PATH=$DIR/php/bin:$PATH">>/etc/profile
			source /etc/profile
		\cp $DIR/downloads/php-5.4.44/sapi/fpm/init.d.php-fpm /etc/init.d/php-fpm
		\cp  /app/php/etc/php-fpm.conf.default /app/php/etc/php-fpm.conf
			chmod +x /etc/init.d/php-fpm
			chkconfig --add php-fpm
			chkconfig php-fpm on
			service php-fpm restart
}

function install_mysql(){
	cd $DIR/downloads
		rm mysql-5.5.49.tar.gz -f
		wget http://59.83.33.68/tizone/mysql-5.5.49.tar.gz
		tar zxvf mysql-5.5.49.tar.gz
		cd mysql-5.5.49
			cmake . -DCMAKE_INSTALL_PREFIX=$DIR/mysql/  -DMYSQL_DATADIR=$DIR/mysql/data  -DWITH_INNOBASE_STORAGE_ENGINE=1  -DMYSQL_TCP_PORT=3306  -DMYSQL_UNIX_ADDR=$DIR/mysql/data/mysql.sock  -DMYSQL_USER=mysql  -DWITH_DEBUG=0  
			make
			make install
		cd $DIR/mysql/
			groupadd mysql
			useradd mysql -g mysql
			chown -R mysql .
			chgrp -R mysql .
			scripts/mysql_install_db --user=mysql  --basedir=$DIR/mysql --datadir=$DIR/mysql/data
			chown -R root .   
			chown -R mysql ./data  
		\cp $DIR/mysql/support-files/my-medium.cnf /etc/my.cnf
		\cp $DIR/mysql/support-files/mysql.server /etc/init.d/mysqld  
			chmod +x /etc/init.d/mysqld  
			chkconfig --add mysqld
			chkconfig mysqld on
			service mysqld start
		echo -e "export PATH=$DIR/mysql/bin:$PATH">>/etc/profile
			source /etc/profile
		PASSWORD=$(whiptail --title "Input Mysql Password" --passwordbox "Enter your password and choose Ok to continue.\n" 10 60 3>&1 1>&2 2>&3)
			mysqladmin -uroot password "$password"
}

function install_phpize(){
	cd $DIR/downloads
		rm 2.2.7.tar.gz -f
		wget http://59.83.33.68/tizone/2.2.7.tar.gz
		tar zxvf 2.2.7.tar.gz
		cd phpredis-2.2.7
			$DIR/php/bin/phpize
			./configure --with-php-config=$DIR/php/bin/php-config
			make
			make install
		cat>>$DIR/php/lib/php.ini<<EOF
[extension]
extension_dir = "$DIR/php/lib/php/extensions/no-debug-non-zts-20100525"
extension=redis.so
EOF
		service php-fpm restart
}

function install_redis(){
	cd $DIR/downloads
		rm redis-2.6.17.tar.gz -f
		wget http://59.83.33.68/tizone/redis-2.6.17.tar.gz
		tar zxvf redis-2.6.17.tar.gz
		cd redis-2.6.17
			make
			make install
}

function install_libevent(){
	cd $DIR/downloads
		rm -f libevent-2.0.22-stable.tar.gz
		wget http://59.83.33.68/tizone/libevent-2.0.22-stable.tar.gz
		tar zxvf libevent-2.0.22-stable.tar.gz
		cd libevent-2.0.22-stable
			./configure --prefix=/usr
			make
			make install
}

function install_memcached(){
	install_libevent
	cd $DIR/downloads
		rm memcached-1.4.25.tar.gz -f
		wget http://59.83.33.68/tizone/memcached-1.4.25.tar.gz
		tar zxvf memcached-1.4.25.tar.gz
		cd memcached-1.4.25
			./configure --with-libevent=/usr
			make
			make install
}

function install_mongodb(){
	cd $DIR/downloads
		rm mongodb-linux-x86_64-rhel62-3.2.6.tgz -f
		wget http://59.83.33.68/tizone/mongodb-linux-x86_64-rhel62-3.2.6.tgz
		tar xvf mongodb-linux-x86_64-rhel62-3.2.6.tgz
		groupadd mongodb
		useradd mongodb -g mongodb
		mv mongodb-linux-x86_64-rhel62-3.2.6 mongodb
		mkdir -p $DIR/mongodb/data
		mkdir -p $DIR/mongodb/logs
}

#web服务器:tengine 2.1.2
#源码位置:$DIR/tengine-2.1.2
#安装位置:$DIR/tengine
#启动方式:service nginx start

#php:php 5.4
#源码位置:$DIR/php-5.4.44
#安装位置:$DIR/php
#php-fpm位置:$DIR/php/sbin/php-fpm
#启动方式:service php-fpm start

#php:php-redis
#源码位置:$DIR/phpredis-2.2.7
#安装位置:$DIR/php/lib/php/extensions/no-debug-non-zts-20100525

#数据库:mysql 5.5
#源码位置:$DIR/mysql-5.5.49
#安装位置:$DIR/mysql
#数据位置:$DIR/mysql/data
#启动方式:service mysqld start

#数据库:Redis 2.6.17
#源码位置:$DIR/redis-2.6.17
#安装位置:/usr/local/bin
#启动方式:redis-server

#高速缓存:memcached 1.4.25
#源码位置:$DIR/memcached-1.4.25
#安装位置:/usr/local/bin/memcached
#启动方式:service memcached start

#数据库:mongodb 3.2.6
#安装位置:$DIR/mongodb
#数据位置:$DIR/mongodb/data
#日志位置:$DIR/mongodb/logs
#启动方式:$DIR/mongodb/bin/mongod --dbpath=$DIR/mongodb/data --logpath $DIR/mongodb/logs/log.log -fork 
#wget https://files.phpmyadmin.net/phpMyAdmin/4.4.15.6/phpMyAdmin-4.4.15.6-all-languages.tar.gz

start_all
whiptail --title "

Congratulations" --msgbox "You selected package has been successfully installed." 10 60